package tk.winshu.common.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.UndeclaredThrowableException;

/**
 * @author Kevin
 * @since 2019/10/06.
 */
public class ExceptionUtil {

    private ExceptionUtil() {
    }

    public static Throwable unwrapThrowable(Throwable wrapped) {
        Throwable unwrapped = wrapped;

        while (true) {
            while (!(unwrapped instanceof InvocationTargetException)) {
                if (!(unwrapped instanceof UndeclaredThrowableException)) {
                    return unwrapped;
                }
                unwrapped = ((UndeclaredThrowableException) unwrapped).getUndeclaredThrowable();
            }
            unwrapped = ((InvocationTargetException) unwrapped).getTargetException();
        }
    }
}
